import cv2

def maskingImage(img,thresholdL,thresholdU):
    imgF = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(imgF,thresholdL,thresholdU)
    return mask

def nothing(pam):
    pass

cv2.namedWindow("orgi")
cv2.namedWindow("mask")
cv2.namedWindow("panel")

cv2.createTrackbar("low1","panel",0,255,nothing)
cv2.createTrackbar("low2","panel",0,255,nothing)
cv2.createTrackbar("low3","panel",0,255,nothing)
cv2.createTrackbar("up1","panel",0,255,nothing)
cv2.createTrackbar("up2","panel",0,255,nothing)
cv2.createTrackbar("up3","panel",0,255,nothing)


cam = cv2.VideoCapture(0)
while True:
    (r,img) = cam.read()
    img = cv2.flip(img,-1)
    img = cv2.resize(img,(img.shape[1]//2,img.shape[0]//2))
    thL = [0,0,0]
    thU = [0,0,0]
    thL[0] = cv2.getTrackbarPos("low1","panel")
    thL[1] = cv2.getTrackbarPos("low2","panel")
    thL[2] = cv2.getTrackbarPos("low3","panel")

    thU[0] = cv2.getTrackbarPos("up1","panel")
    thU[1] = cv2.getTrackbarPos("up2","panel")
    thU[2] = cv2.getTrackbarPos("up3","panel")    
    mask = maskingImage(img,(thL[0],thL[1],thL[2]),(thU[0],thU[1],thU[2]))
    cv2.imshow("orgi",img)
    cv2.imshow("mask",mask)

    key = cv2.waitKey(1)
    if key == ord('q'):
        break 
